package com.ruoyi.zhjd.exp.modules.laboratorydevice.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ValidatorUtils;
import com.ruoyi.zhjd.common.enums.BizCode;
import com.ruoyi.zhjd.exp.modules.laboratorydevice.dto.LaboratoryDeviceDto;
import com.ruoyi.zhjd.exp.modules.laboratorydevice.entity.LaboratoryDeviceEntity;
import com.ruoyi.zhjd.exp.modules.laboratorydevice.service.LaboratoryDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: LaboratoryDeviceController
 * @Description: 实验室设备控制器
 * @author linhuawei
 * @since 2021-05-17
 */
@RestController
@RequestMapping("/api/laboratoryDevice")
public class LaboratoryDeviceController {

    @Autowired
    private LaboratoryDeviceService laboratoryDeviceService;

    /**
     * @Title: page
     * @Description: 获取实验室设备分页列表
     * @param params 查询条件
     * @return Result 实验室设备分页列表
     */
//    //
    @GetMapping("/page")
    public R<IPage<LaboratoryDeviceDto>> page(@RequestParam Map<String, Object> params) {
        IPage<LaboratoryDeviceDto> page = laboratoryDeviceService.page(params);
        return R.ok(page);
    }

    /**
     * @Title: find
     * @Description: 获取信息
     * @param id 信息主键
     * @return Result 信息内容
     */
//    //
    @GetMapping("/find/{id}")
    public R<LaboratoryDeviceEntity> find(@PathVariable("id") String id) {
        LaboratoryDeviceEntity entity = laboratoryDeviceService.getById(id);
        if (null == entity) {
            return R.fail();
        }
        return R.ok(entity);
    }

    /**
     * @Title: save
     * @Description: 添加信息
     * @param entity 信息内容
     * @return Result 添加结果
     */
//    //
    @PostMapping("/save")
    public R<String> save(@RequestBody LaboratoryDeviceEntity entity) {
        ValidatorUtils.validate(entity, AddGroup.class);
        laboratoryDeviceService.save(entity);
        return R.ok();
    }

    /**
     * @Title: update
     * @Description: 修改信息
     * @param entity 信息内容
     * @return Result 修改结果
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody LaboratoryDeviceEntity entity) {
        ValidatorUtils.validate(entity, EditGroup.class);
        laboratoryDeviceService.updateById(entity);
        return R.ok();
    }

    /**
     * @Title: save
     * @Description: 绑定设备终端
     * @param jsonObject 信息内容
     * @return Result 添加结果
     */
//    //
    @PostMapping("/bindCode")
    public R<String> bindCode(@RequestBody JSONObject jsonObject) {
        String code = jsonObject.get("code").toString();
        String id = jsonObject.get("id").toString();

        if (ObjectUtils.isEmpty(id)) {
            throw new ServiceException( "试验设备不存在");
        }
        if (ObjectUtils.isEmpty(code)) {
            throw new ServiceException( "终端编号不存在");
        }
        if (3 != code.length()) {
            throw new ServiceException( "终端编号格式错误");
        }

        laboratoryDeviceService.bindCode(id, code);
        return R.ok();
    }

    /**
     * @Title: unBindCode
     * @Description: 解除绑定设备终端
     * @param jsonObject 信息内容
     * @return Result 添加结果
     */
//    //
    @PostMapping("/unBindCode")
    public R<String> unBindCode(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.get("id").toString();

        if (ObjectUtils.isEmpty(id)) {
            throw new ServiceException( "试验设备不存在");
        }

        laboratoryDeviceService.unBindCode(id);
        return R.ok();
    }

    /**
     * @Title: delete
     * @Description: 删除信息
     * @param id 信息主键
     * @return Result 删除结果
     */
//    //
    @PostMapping("/delete/{id}")
    public R<String> delete(@PathVariable("id") String id) {
        laboratoryDeviceService.removeById(id);
        return R.ok();
    }

    /**
     * @Title: delete
     * @Description: 删除信息
     * @param ids 信息主键
     * @return Result 删除结果
     */
//    //
    @PostMapping("/delete")
    public R<String> delete(@RequestBody List<String> ids) {
        laboratoryDeviceService.removeByIds(ids);
        return R.ok();
    }

    /**
     * @Title: list
     * @Description: 获取实验室设备下拉框
     * @param params 查询条件
     * @return Result 实验室设备下拉框数据
     */
    @GetMapping("/list")
    public R<List<LaboratoryDeviceEntity>> list(@RequestParam Map<String, Object> params) {
        QueryWrapper wrapper = new QueryWrapper<LaboratoryDeviceEntity>();
        if(StringUtils.isNotBlank((String)params.get("name"))){
            String[] names = ((String)params.get("name")).split(",");
            for(String name : names) {
                wrapper.like("name",name);
                wrapper.or();
            }
        }
        if(StringUtils.isNotBlank((String)params.get("baseCode"))){
            String baseCode = ((String)params.get("baseCode"));
            wrapper.eq("base_code",baseCode);
        }
        if(StringUtils.isNotBlank((String)params.get("type"))){
            String type = ((String)params.get("type"));
            wrapper.eq("type",type);
        }
        wrapper.orderByAsc("name");
        List<LaboratoryDeviceEntity> list = laboratoryDeviceService.list(wrapper);
        return R.ok(list);
    }
}
